<?php
    /**
     * Created by: shauan.
     * Date: 25/06/14 - 19:25
     *
     * ContasPagar Controller
     */
    App::uses('AppController','Controller');

    /**
     * Property Class Contas a Pagar
     * @property Expense Expense
     * @property Account Account
     * @property PayReceive PayReceive
     * @property mixed request
     * @method mixed loadModel
     * @method mixed set
     * @method mixed redirect
     */
    class ContasPagarController extends AppController {

        public function index(){
            $this->layout='account';
            $this->loadModel('PayReceive');
            $this->Filter->addFilters(
                array(
                    'filter1' => array(
                        'PayReceive.descricao' => array(
                            'operator' => 'LIKE',
                            'value' => array(
                                'before' => '%', // opcional
                                'after'  => '%'  // opcional
                            )
                        ),
                    ),
                )
            );

            $Ano = date("Y");
            $Mes = date("m");
            $option = array(
                'conditions'=>array(
                    'PayReceive.tipo_conta'=>'P',
                    'PayReceive.token_user'=>$this->tokenUser(),
                    'YEAR(PayReceive.data_vencimento)'=>$Ano,
                    'MONTH(PayReceive.data_vencimento)'=>$Mes
                ),
                'order'=>'PayReceive.data_vencimento asc'
            );

            if($this->Filter->getConditions() != null){
                $outros = array();
                foreach($this->Filter->getConditions() as $key=>$value){
                    $outros[$key]=$value;
                }
                $outros['PayReceive.tipo_conta']='P';
                $outros['PayReceive.token_user']=$this->tokenUser();
                $option['conditions'] = $outros;
            }
            $contasPagar = $this->PayReceive->find('all',$option);
            $i=0;
            foreach($contasPagar as $value){
                if (!empty($value['PayReceive']['valor_liquido'])) {
                    if($value['PayReceive']['valor_liquido'] != 0){
                        $contasPagar[$i]['PayReceive']['valor_doc'] = $value['PayReceive']['valor_liquido'];
                    }
                }
                $i++;
            }
            $listarAnos = $this->listaAnosConta();
            $ContaMesAtrazadas = $this->contasAtrazadas();
            $valorConta = $this->valoresPagar();
            $this->set(compact('contasPagar','ContaMesAtrazadas','valorConta','Mes','Ano','listarAnos'));
            $mnContasPagar = 'active';
            $this->set(compact('mnContasPagar'));
        }

        public function contasAtrazadas(){
            $this->loadModel('PayReceive');
            $anoAtual = date('Y');
            $options = array(
                'conditions'=>array(
                    'PayReceive.token_user'=>$this->tokenUser(),
                    'PayReceive.tipo_conta'=>'P',
                    "YEAR(PayReceive.data_vencimento)"=>$anoAtual,
                    "PayReceive.data_pagamento is null"
                ),
                'fields'=>array(
                    'PayReceive.id',
                    'PayReceive.data_vencimento',
                )
            );
            $contasPagar = $this->PayReceive->find('all',$options);
            $mesAtrazados = array();
            $i=1;
            foreach($contasPagar as $value){
                $mes = date('m/Y',strtotime($value['PayReceive']['data_vencimento']));
                $_mes = strtotime(date('Y-m',strtotime($value['PayReceive']['data_vencimento'])));
                $mesAtual = strtotime(date('Y-m'));
                if($_mes < $mesAtual){
                    $meses = isset($mesAtrazados[$mes])? $mesAtrazados[$mes]: 0 ;
                    $mesAtrazados[$mes] =  $meses + 1;
                }else{
                    $mesAtrazados[$mes] =  0;
                }

            }
            return $mesAtrazados;
        }

        public function novaContaPagar(){
            header('Content-Type: application/json');
            $this->autoRender = false;
            $this->layout = false;
            $this->loadModel('Expense');
            $this->loadModel('PayReceive');
            $this->loadModel('Account');
            $this->loadModel('ExpenseGroup');
            $msg=null;
            $error = null;
            if($this->request->is('post')){
                $this->request->data['PayReceive']['tipo_conta'] = "P";
                if(isset($this->request->data['PayReceive']['parcelamento']) && $this->request->data['PayReceive']['parcelamento'] == true){
                    $nparcelas = $this->request->data['PayReceive']['nparcelas'];
                    $frequencia = $this->request->data['PayReceive']['frequencia'];
                    $dataVencimento = $this->request->data['PayReceive']['data_vencimento'];
                    $valor_doc = $this->request->data['PayReceive']['valor_doc'];
                    $descricao = $this->request->data['PayReceive']['descricao'];
                    $count = 1;
                    if(isset($this->request->data['PayReceive']['e_replica']) && $this->request->data['PayReceive']['e_replica'] == 1){
                        $count = 2;
                        $this->request->data['PayReceive']['descricao'] = $this->request->data['PayReceive']['descricao']." 1/{$nparcelas}";
                        $this->request->data = $this->validaFormInsert($this->request->data);
                        $this->PayReceive->create();
                        $this->PayReceive->save($this->request->data);
                    }
                    for($i=$count;$i<=$nparcelas;$i++){
                        $this->request->data['PayReceive']['descricao'] = $descricao." {$i}/{$nparcelas}";
                        $dataVencimento = $this->calcularParcelas($dataVencimento,1,$frequencia);
                        $this->request->data['PayReceive']['data_vencimento'] = $dataVencimento;
                        $this->request->data['PayReceive']['valor_doc'] = $valor_doc;
                        $this->request->data = $this->validaFormInsert($this->request->data);
                        if(isset($this->request->data['PayReceive']['pago']) && $this->request->data['PayReceive']['pago'] == 1){
                            $account_id = $this->request->data['PayReceive']['account_id'];
                            $updateAccount = array('account_id'=>$account_id,'valor'=>$this->request->data['PayReceive']['valor_pago']);
                            $this->updateAccount($updateAccount);
                        }
                        $this->PayReceive->create();
                        $this->PayReceive->save($this->request->data);
                    }
                    $msg = "Seu registro foi salvo com sucesso.";
                    $error = false;
                }elseif(!$this->validaFormInsert($this->request->data)==false){
                    $this->request->data = $this->validaFormInsert($this->request->data);
                    if(isset($this->request->data['PayReceive']['pago']) && $this->request->data['PayReceive']['pago'] == 1){
                        $account_id = $this->request->data['PayReceive']['account_id'];
                        $updateAccount = array('account_id'=>$account_id,'valor'=>$this->request->data['PayReceive']['valor_pago']);
                        $this->updateAccount($updateAccount);
                    }
                    $this->PayReceive->create();
                    if($this->PayReceive->save($this->request->data)){
                        $msg = "Seu registro foi salvo com sucesso.";
                        $error = false;
                    }else{
                        $msg = "Houve um error ao tentar salvar os dados. Verifique.";
                        $error = true;
                    }
                }
            }
            $optionExpenses = array(
                'conditions'=>array(
                    'Expense.token_user'=>$this->tokenUser(),
                ),
            );
            $expenses = $this->Expense->find('list',$optionExpenses);
            $optionAccount = array(
                'conditions'=>array(
                    'Account.token_user'=>$this->tokenUser(),
                ),
            );
            $accounts = $this->Account->find('list',$optionAccount);
            $this->set(compact('expenses','accounts'));
            $view = new View($this,false);
            $returnView = $view->render();
            $renderJson = array('view'=>$returnView,'message'=>$msg,'error'=>$error);
            return json_encode($renderJson);
        }

        public function editContaPagar($id=null){
            header('Content-Type: application/json');
            $this->autoRender = false;
            $this->layout = false;
            $this->loadModel('Expense');
            $this->loadModel('PayReceive');
            $this->loadModel('Account');
            $this->loadModel('ExpenseGroup');
            $msg=null;
            $error = null;
            if (!$this->request->is(array('post', 'put'))) {
                if (!$this->PayReceive->exists($id)) {
                    $msg = $this->msgNotFould();
                    $error = true;
                    $renderJson = array('message'=>$msg,'error'=>$error);
                    return json_encode($renderJson);
                }
            }
            if ($this->request->is(array('post', 'put'))) {
                if(!$this->validaFormInsert($this->request->data)==false){
                    $this->request->data = $this->validaFormInsert($this->request->data);
                    if(isset($this->request->data['PayReceive']['pago']) && $this->request->data['PayReceive']['pago'] == 1){
                        $account_id = $this->request->data['PayReceive']['account_id'];
                        $updateAccount = array('account_id'=>$account_id,'valor'=>$this->request->data['PayReceive']['valor_pago']);
                        $this->updateAccount($updateAccount);
                    }
                    if($this->PayReceive->save($this->request->data)){
                        $msg = "Seu registro foi atualizado com sucesso.";
                        $error = false;
                    }else{
                        $msg = "Houve um error ao tentar atualizar os dados. Verifique.";
                        $error = true;
                    }
                }
            }else{
                $optionExpenses = array(
                    'conditions'=>array(
                        'Expense.token_user'=>$this->tokenUser(),
                    ),
                );
                $expenses = $this->Expense->find('list',$optionExpenses);
                $optionAccount = array(
                    'conditions'=>array(
                        'Account.token_user'=>$this->tokenUser(),
                    ),
                );
                $accounts = $this->Account->find('list',$optionAccount);
                $options = array('conditions' => array('PayReceive.token_user'=>$this->tokenUser(),'PayReceive.tipo_conta'=>'P','PayReceive.' . $this->PayReceive->primaryKey => $id));
                $dados = $this->PayReceive->find('first', $options);
                $dados['PayReceive']['valor_doc'] = $this->money($dados['PayReceive']['valor_liquido']);
                $dados['PayReceive']['data_vencimento'] = date('d/m/Y',strtotime($dados['PayReceive']['data_vencimento']));
                $this->request->data = $dados;
                $this->set(compact('expenses','accounts'));
            }
            $view = new View($this,false);
            $returnView = $view->render();
            $renderJson = array('view'=>$returnView,'message'=>$msg,'error'=>$error);

            return json_encode($renderJson);
        }

        private function validaFormInsert($param=null){
            if(!empty($param) && $param != null){
                $param['PayReceive']['token_user'] = $this->tokenUser();
                $param['PayReceive']['valor_liquido'] = $this->noMoney($param['PayReceive']['valor_doc']);
                $param['PayReceive']['valor_doc'] = $this->noMoney($param['PayReceive']['valor_doc']);
                $param['PayReceive']['data_vencimento'] = $this->dateDBFormat($param['PayReceive']['data_vencimento']);
                if (isset($param['PayReceive']['pago']) && $param['PayReceive']['pago'] == 1) {
                    $param['PayReceive']['valor_pago'] = $this->noMoney($param['PayReceive']['valor_pago']);
                    $param['PayReceive']['data_pagamento'] = $this->dateDBFormat($param['PayReceive']['data_pagamento']);
                    if($param['PayReceive']['valor_pago'] < $param['PayReceive']['valor_doc']){
                        unset($param['PayReceive']['data_pagamento']);
                        $param['PayReceive']['valor_liquido'] = $param['PayReceive']['valor_doc'] - $param['PayReceive']['valor_pago'];
                    }
                }else{
                    unset($param['PayReceive']['valor_pago']);
                    unset($param['PayReceive']['data_pagamento']);
                }
                return $param;
            }else{
                return false;
            }
        }

        public function parcelamento(){
            $this->autoRender = false;
            $this->layout = false;
            $dados=null;
            if($this->request->is('post')){
                $dados = $this->request->data;
            }
            $this->set(compact('dados'));
            $view = new View($this,false);
            $returnView = $view->render();
            $renderJson = array('view'=>$returnView);
            /** @noinspection PhpInconsistentReturnPointsInspection */
            return json_encode($renderJson);
        }

        public function valoresPagar(){
            $this->loadModel('PayReceive');
            $contasPagar = $this->PayReceive->find('all',array('conditions'=>array('PayReceive.token_user'=>$this->tokenUser(),'PayReceive.tipo_conta'=>'P'),'fields'=>array('PayReceive.valor_doc','PayReceive.valor_liquido','PayReceive.valor_pago','PayReceive.data_vencimento','PayReceive.data_pagamento')));
            $result = array();
            $total = 0;
            $pagas = 0;
            $atrazado = 0;
            $apagar = 0;
            $dataHoje = $this->converteData(date('Y-m-d'));
            $IniMes = $this->converteData(date('Y-m-01'));
            $fimMes = $this->converteData(date('Y-m-t'));
            foreach($contasPagar as $value){
                $total = $total + $value['PayReceive']['valor_doc'];
                $pagas = $pagas + $value['PayReceive']['valor_pago'];
                $dataVenvimento = $this->converteData($value['PayReceive']['data_vencimento']);
                $dataPagamento = $this->converteData($value['PayReceive']['data_pagamento']);
                $result['PayReceive']['totalPagar'] = $this->money($total);
                $result['PayReceive']['pagas'] = $this->money($pagas);
                if($dataVenvimento < $dataHoje && ($dataPagamento == null || empty($dataPagamento))){
                    if($value['PayReceive']['data_pagamento'] == null){
                        $atrazado = $atrazado + $value['PayReceive']['valor_liquido'];
                        $result['PayReceive']['atrazado'] = $this->money($atrazado);
                    }else{
                        $result['PayReceive']['atrazado'] = $this->money(0);
                    }
                }

                if($dataVenvimento >= $IniMes && $dataVenvimento <= $fimMes){
                    $apagar = $apagar + $value['PayReceive']['valor_liquido'];
                    $result['PayReceive']['apagar'] = $this->money($apagar);
                }
            }

            return $result;
        }

        public function pagarConta(){
            header('Content-Type: application/json');
            $this->autoRender = false;
            $this->layout = false;
            $message = '';
            $error = true;
            if($this->request->is('post')){
                $this->loadModel('PayReceive');
                $this->loadModel('Account');
                $param = array('Ano'=>$this->request->data['Ano'],'Mes'=>$this->request->data['Mes']);

                if($this->PayReceive->exists($this->request->data['idConta'])){
                    $options = array('conditions' => array('PayReceive.token_user'=>$this->tokenUser(),'PayReceive.tipo_conta'=>'P','PayReceive.' . $this->PayReceive->primaryKey => $this->request->data['idConta']),'fields'=>array('PayReceive.id','PayReceive.valor_pago','PayReceive.data_pagamento','PayReceive.valor_doc','PayReceive.valor_liquido','PayReceive.account_id'));
                    $this->request->data = $this->PayReceive->find('first', $options);
                    $valor_liquido = $this->request->data['PayReceive']['valor_liquido'];
                    $valor_doc = $this->request->data['PayReceive']['valor_doc'];
                    $account_id = $this->request->data['PayReceive']['account_id'];
                    $this->request->data['PayReceive']['valor_pago'] = $this->request->data['PayReceive']['valor_doc'];
                    $this->request->data['PayReceive']['valor_liquido'] = $this->request->data['PayReceive']['valor_doc'] - $this->request->data['PayReceive']['valor_pago'];
                    $this->request->data['PayReceive']['data_pagamento'] = $this->dateDBFormat(date('d/m/Y'));
                    unset($this->request->data['PayReceive']['valor_doc']);
                    $updateAccount = array('account_id'=>$account_id,'valor'=>$valor_doc,'valor_pago'=>$valor_liquido);
                    if($this->updateAccount($updateAccount)){
                        if($this->PayReceive->save($this->request->data)){
                            $message = 'Conta recebida com sucesso.';
                            $error = false;
                        }else{
                            $message = 'Houve um error ao tentar efetuar o pagar a conta.';
                            $error = true;
                        }
                    }else{
                        $message = 'Houve um error ao tentar efetuar o pagar a conta.';
                        $error = true;
                    }
                }
                $view = new View($this,false);
                $litaContas = $this->listConta($param);
                $valorContas = $this->valoresConta();
            }
            $renderJson = array('lista'=>$litaContas,'valores'=>$valorContas,'message'=>$message,'error'=>$error);
            return json_encode($renderJson);
        }

        public function estornarConta(){
            header('Content-Type: application/json');
            $this->autoRender = false;
            $this->layout = false;
            $message = '';
            $error = true;
            if($this->request->is('post')){
                $this->loadModel('PayReceive');
                $param = array('Ano'=>$this->request->data['Ano'],'Mes'=>$this->request->data['Mes']);
                if($this->PayReceive->exists($this->request->data['idConta'])){
                    $options = array('conditions' => array('PayReceive.token_user'=>$this->tokenUser(),'PayReceive.tipo_conta'=>'P','PayReceive.' . $this->PayReceive->primaryKey => $this->request->data['idConta']),'fields'=>array('PayReceive.id','PayReceive.valor_pago','PayReceive.data_pagamento','PayReceive.valor_doc','PayReceive.valor_liquido','PayReceive.account_id'));
                    $this->request->data = $this->PayReceive->find('first', $options);
                    $valor_doc = $this->request->data['PayReceive']['valor_doc'];
                    $account_id = $this->request->data['PayReceive']['account_id'];
                    $this->request->data['PayReceive']['valor_pago'] = null;
                    $this->request->data['PayReceive']['data_pagamento'] = null;
                    $this->request->data['PayReceive']['valor_liquido'] = $this->request->data['PayReceive']['valor_doc'];
                    unset($this->request->data['PayReceive']['valor_doc']);
                    $updateAccount = array('account_id'=>$account_id,'valor'=>$valor_doc,'estorno'=>true);
                    if($this->updateAccount($updateAccount)){
                        if($this->PayReceive->save($this->request->data)){
                            $message = 'Conta estornada com sucesso.';
                            $error = false;
                        }else{
                            $message = 'Houve um error ao tentar efetuar o pagar a conta.';
                            $error = true;
                        }
                    }else{
                        $message = 'Houve um error ao tentar efetuar o pagar a conta.';
                        $error = true;
                    }
                }
                $view = new View($this,false);
                $litaContas = $this->listConta($param);
                $valorContas = $this->valoresConta();
            }
            $renderJson = array('lista'=>$litaContas,'valores'=>$valorContas,'message'=>$message,'error'=>$error);
            return json_encode($renderJson);
        }

        public function listaContaMes(){
            header('Content-Type: application/json');
            $this->autoRender = false;
            $this->layout = false;
            $message = '';
            $error = true;
            if($this->request->is('post')){
                $view = new View($this,false);
                $tabelaBtnMes = $this->tabelaBtnMes($this->request->data);
                $litaContas = $this->listConta($this->request->data);
                $error = false;
            }
            $renderJson = array('lista'=>$litaContas,'tabelaBtnMes'=>$tabelaBtnMes,'message'=>$message,'error'=>$error);
            return json_encode($renderJson);
        }

        private function listConta($param = null){
            $this->layout = false;
            $this->autoRender = false;
            $this->loadModel('PayReceive');
            $Ano = $param['Ano'];
            $Mes = $param['Mes'];
            $option = array(
                'conditions'=>array(
                    'PayReceive.token_user'=>$this->tokenUser(),
                    'PayReceive.tipo_conta'=>'P',
                    'YEAR(PayReceive.data_vencimento)'=>$Ano,
                    'MONTH(PayReceive.data_vencimento)'=>$Mes
                ),
                'order'=>'PayReceive.data_vencimento asc'
            );
            $ContaMesAtrazadas = $this->contasAtrazadas();
            $contasPagar = $this->PayReceive->find('all',$option);
            $i=0;
            foreach($contasPagar as $value){
                if (!empty($value['PayReceive']['valor_liquido'])) {
                    if($value['PayReceive']['valor_liquido'] != 0){
                        $contasPagar[$i]['PayReceive']['valor_doc'] = $value['PayReceive']['valor_liquido'];
                    }
                }
                $i++;
            }
            $listarAnos = $this->listaAnosConta();
            $this->set(compact('contasPagar','ContaMesAtrazadas','Ano','Mes','listarAnos'));
            $view = new View($this,false);
            $returnView = $view->render('/ContasPagar/_list');
            return $returnView;
        }

        private function tabelaBtnMes($param){
            $this->layout = false;
            $this->autoRender = false;
            $Mes = $param['Mes'];
            $Ano = $param['Ano'];
            $ContaMesAtrazadas = $this->contasAtrazadas();
            $listarAnos = $this->listaAnosConta();
            $this->set(compact('Mes','Ano','ContaMesAtrazadas','listarAnos'));
            $view = new View($this,false);
            $returnView = $view->render('/Elements/_tabelaBtnMes');
            return $returnView;
        }

        private function valoresConta(){
            $this->layout = false;
            $this->autoRender = false;
            $valorConta = $this->valoresPagar();
            $this->set(compact('valorConta'));
            $view = new View($this,false);
            $returnView = $view->render('/ContasPagar/_infoContaPagar');
            return $returnView;
        }

        public function retornaValor(){
            header('Content-Type: application/json');
            $this->autoRender = false;
            $this->layout = false;
            $view = new View($this,false);
            $renderJson = array();
            return json_encode($renderJson);
        }

        public function delete($id = null) {
            $this->loadModel('PayReceive');
            $this->PayReceive->id = $id;
            if (!$this->PayReceive->exists()) {
                $this->msgNotFould();
                return $this->redirect(array('action' => 'index'));
            }
            $this->request->allowMethod('post', 'delete');
            if ($this->PayReceive->delete()) {
                $this->msgDelete();
            } else {
                $this->msgDelete(false);
            }
            return $this->redirect(array('action' => 'index'));
        }

        public function gerarReplicar(){
            header('Content-Type: application/json');
            $this->autoRender = false;
            $this->layout = false;
            $dados = array();
            if($this->request->is('post')){
                if($this->request->data['PayReceive']['parcelamento'] == true){
                    $nparcelas = $this->request->data['PayReceive']['nparcelas'];
                    $frequencia = $this->request->data['PayReceive']['frequencia'];
                    $dataVencimento = $this->request->data['PayReceive']['data_vencimento'];
                    $count = 1;
                    if(isset($this->request->data['PayReceive']['e_replica']) && $this->request->data['PayReceive']['e_replica'] == 1){
                        $count = 2;
                        $dados[1]['dataVencimento'] = $dataVencimento;
                        $dados[1]['valor'] = $this->request->data['PayReceive']['valor_doc'];
                    }
                    for($i=$count;$i<=$nparcelas;$i++){
                        $dataVencimento = $this->calcularParcelas($dataVencimento,1,$frequencia);
                        $dados[$i]['dataVencimento'] = $dataVencimento;
                        $dados[$i]['valor'] = $this->request->data['PayReceive']['valor_doc'];
                    }
                }
            }
            $renderJson = array('dados'=>$dados);
            return json_encode($renderJson);
        }

        public function listaAnosConta(){
            $contasPagar = $this->PayReceive->find('all',array('conditions'=>array('PayReceive.token_user'=>$this->tokenUser(),'PayReceive.tipo_conta'=>'P'),'fields'=>'PayReceive.data_vencimento'));
            $lista_anos = array();
            if(count($contasPagar) >0){
                foreach($contasPagar as $value){
                    $anos = date('Y',strtotime($value['PayReceive']['data_vencimento']));
                    $lista_anos[$anos] = $anos;
                }
            }else{
                $lista_anos[date('Y')] = date('Y');
            }
            return $lista_anos;
        }

        public function updateAccount($param=null){
            $this->autoRender = false;
            $this->layout = false;
            $param = array_merge(array(
                'account_id'=>null,
                'valor'=>0,
                'valor_pago'=>0,
                'estorno'=>false
            ),$param);
            $this->loadModel('Account');
            $this->Account->id = $param['account_id'];
            if($this->Account->exists($param['account_id'])){
                $options = array('conditions' => array('Account.' . $this->Account->primaryKey => $param['account_id']),'fields'=>array('Account.id','Account.valor'));
                $accounts = $this->Account->find('first', $options);
                $valorAccount = (float) $accounts["Account"]['valor'];
                if($param['valor_pago'] == 0){
                    $valorPayReceive = (float) $param['valor'];
                }else{
                    $valorPayReceive = (float) $param['valor_pago'];
                }

                if(isset($param['estorno']) && $param['estorno'] == false){
                    $total = $valorAccount - $valorPayReceive;
                }else{
                    $total = $valorAccount + $valorPayReceive;
                }

                if($this->Account->saveField('valor',$total)){
                    return true;
                }else{
                    return false;
                }
            }
        }

        public function DeleteAll(){
            $this->autoRender = false;
            $this->layout = false;
            $this->loadModel('PayReceive');
            if($this->request->is('post')){
                $i=1;
                $j=1;
                foreach($this->request->data as $key=>$value){
                    $this->PayReceive->id = $value;
                    if (!$this->PayReceive->exists()) {
                        $msg = "Houve um error ao excluir o registro.";
                        $error = false;
                        $renderJson = array('message'=>$msg,'error'=>$error);
                    }

                    $this->request->allowMethod('post', 'delete');
                    if($this->PayReceive->delete()){
                        $msg = $i." registro foram excluido com sucesso!";
                        if($i==1){
                            $msg = "O registro foi excluido com sucesso!";
                        }
                        $error = false;
                        $renderJson = array('message'=>$msg,'error'=>$error,'id'=>$value);
                    }else{
                        $msg = "Houve um error ao excluir {$j} registro(s).";
                        $error = false;
                        $renderJson = array('message'=>$msg,'error'=>$error);
                        $j++;
                    }
                    $i++;
                }
                return json_encode($renderJson);
            }
        }
    }